package com.sora.org.controller;

import com.sora.basic.util.AjaxResult;
import com.sora.basic.util.PageList;
import com.sora.org.domain.Department;
import com.sora.org.query.DepartmentQuery;
import com.sora.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
@Api(value="部门接口类",description = "处理部门的CRUD操作")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/department/1
    @GetMapping("/{id}")
    @ApiOperation(value = "接口：查询单个")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //http://localhost:8080/department
    @GetMapping
    @ApiOperation(value = "接口：查询所有")
    public List<Department> findAll(){
        return departmentService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "接口：删除")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或者修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "接口：添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询+高级查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "接口：分页查询+高级查询")
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.pageQuery(departmentQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "接口：批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 部门树
     * @return
     */
    @GetMapping("/deptTree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }

}
